﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{

    public class RushPage : Page
    {
        public UILabel floorNumLabel;
        public UILabel passStarNumLabel;
        public UILabel leftResetNumLabel;
        public UILabel recordStarNumLabel;
        public UILabel floorNumTipLabel;
        public UILabel winConditionLabel;
        public GameObject fightBtn;
        public GameObject resetBtn;

        bool isNeedReward = false;
        bool isNeedAddition = false;
        bool isMazeTraderValid
        {
            get
            {
                return StaticData.isMazeTraderValid;
            }
            set
            {
                StaticData.isMazeTraderValid = value;
            }
        }

        public GameObject topLeft;
        public GameObject topRight;
        public GameObject bottomRight;
        public GameObject bottomCenter;
        public GameObject bottomLeft;

        public UILabel fightPowerLabel;


        int currentFloor;

        bool isResetState = false;

        protected override void DoOpen()
        {
            StaticData.isMazeTraderNeedShow = true;
            Init();
            AskRushInfo();
           // AudioManager.Instance.PlayUIAudioChannel("sakuya_bat_0010");

            onRushResetConfirm += StaticData.funcNavKit.OnRushReset;
        }

        public override void DoReopen()
        {
            ShowBtns();
            AskRushInfo();
        }

        protected override void DoClose()
        {
            StaticData.isMazeTraderNeedShow = true;
            StaticData.rushCurFloorSceneName = string.Empty;
            onRushResetConfirm += StaticData.funcNavKit.OnRushReset;
        }


        void Init() {
            LoadingPanel.Instance.ShowLoading();
            Room.RoomManager.Switch(SceneType.RUSH);
        }

        void AskRushInfo() {
            CSGetRushInfo msg = new CSGetRushInfo();
            NetAPI.SendHttp(OpDefine.CSGetRushInfo, msg, (pack, b) => {
                if (b)
                {
                    SCGetRushInfo _msg = pack.kBody as SCGetRushInfo;
                    Refresh(_msg);
                    StaticData.rushCurFloorNum = _msg.curFloor;
                    StaticData.rushCurWinKey = _msg.winKey;
                    StaticData.rushCurWinValue = _msg.winValue;
                    StaticData.rushCurFloorSceneName = _msg.curFloorSceneName;
                    if (string.IsNullOrEmpty(StaticData.rushCurFloorSceneName))
                    {
                        StaticData.rushCurFloorSceneName = "map_portal_001";
                    }

                    this.currentFloor = _msg.curFloor;
                    this.diamondResetNum = _msg.diamondRestLeftTime;
                    this.diamondResetCost = _msg.diamondRestCostNum;
                    this.freeResetNum = _msg.resetLeftTime;
                    this.maxSweepFloor = _msg.highRecordFloor;

                    if (!StaticData.IsRushBossInfoGot())
                    {
                        CSGetRushEliteBossInfo __msg = new CSGetRushEliteBossInfo();
                        NetAPI.SendHttp(OpDefine.CSGetRushEliteBossInfo, __msg, (_pack, _b) => {
                            if (_b)
                            {
                                SCGetRushEliteBossInfo ___msg = _pack.kBody as SCGetRushEliteBossInfo;
                                StaticData.ReceiveRushEliteBossInfo(___msg);
                            }
                        });
                    }
                }
            });
        }

        public override void OnCoverPageRemove()
        {
            AskRushInfo();
            var rr = Room.RoomManager.currentRoom as Room.RoomRush;
            if (rr != null) {
                if (rr.IsRunning()) {
                    HideBtns();
                }
            }
        }


        public void Refresh(SCGetRushInfo msg) {
            floorNumLabel.text = msg.curFloor.ToString();
            floorNumTipLabel.text = msg.curFloor.ToString();
            winConditionLabel.text = ParseWinCondition(msg.winKey, msg.winValue);
            passStarNumLabel.text = msg.curTotalStar.ToString();
            leftResetNumLabel.text = msg.resetLeftTime.ToString();
            recordStarNumLabel.text = msg.recordTotalStar.ToString();
            fightPowerLabel.text = StaticData.playerData.InstPlayerData.GetFightPower().ToString();

            isNeedAddition = msg.isAdditionNeedGet == 0 ? false : true;
            isNeedReward = msg.isRewardNeedGet == 0 ? false : true;
            isMazeTraderValid = msg.isMazeTraderValid > 0 ? true : false;
            isResetState = msg.isMustReset > 0 ? true : false;

            if (msg.isMustReset > 0)
            {
                resetBtn.SetActive(true);
                fightBtn.SetActive(false);
                bottomRight.SetActive(false);
            }
            else {
                resetBtn.SetActive(false);
                fightBtn.SetActive(true);
                var rr = Room.RoomManager.currentRoom as Room.RoomRush;
                if (rr != null)
                {
                    if (rr.IsRunning())
                    {
                        bottomRight.SetActive(false);
                    }
                    else
                    {
                        bottomRight.SetActive(true);
                    }
                }
            }


            if (isNeedAddition || isNeedReward) {
                if (isNeedReward)
                {
                    AskGetRewardObj();
                }
                else {
                    if (isNeedAddition) {
                        AskGetAttrAddition();
                    }
                }
            }

            if (StaticData.isMazeTraderNeedShow)
            {
                if (isMazeTraderValid)
                {
                    OnMazeTraderBtnClick();
                }
                StaticData.isMazeTraderNeedShow = false;
            }

        }

        public static string ParseWinCondition(int key, int value) {
            switch (key) {
                case 1:
                    return string.Format(TableTextBlo.GetText("win_condition_round_limit"), value);
                case 2:
                    return string.Format(TableTextBlo.GetText("win_condition_max_hp_limit"), value);
                case 3:
                    return string.Format(TableTextBlo.GetText("win_condition_dead_limit"), value);
                default:
                    return TableTextBlo.GetText("win_condition_normal");
            }
        }

        public static string ParseAttributeAdditionName(string str)
        {
            string attribute = "";
            switch (str)
            {
                case "peratt":
                    attribute = Dict.Config.TableConst.Attack;
                    break;
                case "perhp":
                    attribute = Dict.Config.TableConst.Hp;
                    break;
                case "permdef":
                    attribute = Dict.Config.TableConst.MagicDefend;
                    break;
                case "perpdef":
                    attribute = Dict.Config.TableConst.PhyDefend;
                    break;
                default:
                    attribute = str;
                    break;
            }
            return CommonMethod.GetHeroAttributePropName(attribute);
        }


        void AskGetRewardObj() {
            if (isNeedReward) {
                ProtoBuf.Message.CSRushGetSectionReward msg = new CSRushGetSectionReward();
                NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushGetSectionReward, msg, (pack, b) => {
                    if (b)
                    {
                        StaticData.sectionReward = pack.kBody as ProtoBuf.Message.SCRushGetSectionReward;

                        if (StaticData.sectionReward != null)
                        {
                            StaticData.playerData.AddResultObj(StaticData.sectionReward.gain);
                            PageManager.Instance.OpenPage("RushRewardPage", "");
                        }
                    }
                });
            }
        }

        void AskGetAttrAddition() {
            if (isNeedAddition) {
                CSRushGetSectionAddition msg = new CSRushGetSectionAddition();
                NetAPI.SendHttp(OpDefine.CSRushGetSectionAddition, msg, (pack, b) => {
                    if (b)
                    {
                        StaticData.sectionAddition = pack.kBody as ProtoBuf.Message.SCRushGetSectionAddition;
                        if (StaticData.sectionAddition != null)
                        {
                            PageManager.Instance.OpenPage("RushBuyAdditionPage", "");
                        }
                    }
                });
            }
        }

        public void OnFightBtnClick() {
            PageManager.Instance.OpenPage("RushFightReadyPage", "");
        }

        public RushResetConfirmPanel resetPanel;
        int freeResetNum;
        int diamondResetNum;
        int diamondResetCost;

        public void OnResetBtnClick() {
            if (freeResetNum > 0) {
                resetPanel.Enter(freeResetNum);
            }
            else
            {
                if (diamondResetNum > 0)
                {
                    resetPanel.Enter(diamondResetNum, diamondResetCost);
                }
                else
                {
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("rush_reset_time_out"));
                }
            }
        }

        public void OnRushRewardPreviewButtonClick() {
            PageManager.Instance.OpenPage("RushRewardPreviewPage", "");
        }

        public void OnRushAdditionViewButtonClick() {
            PageManager.Instance.OpenPage("RushAdditionViewPage", "");
        }

        public RushSweepToTopConfirmPanel rushSweepToTopConfirmPanel;
        int maxSweepFloor
        {
            get { return StaticData.rushMaxFloor; }
            set { StaticData.rushMaxFloor = value; }
        }

        public void OnRushSweepTopButtonClick()
        {
            if (StaticData.playerData.level >= Dict.Blo.DictConfigBlo.GetRushSweepTopLevelLimit() ||
                StaticData.playerData.vip >= Dict.Blo.DictConfigBlo.GetRushSweepTopVipLevelLimit())
            {
                if (currentFloor <= maxSweepFloor)
                {
                    rushSweepToTopConfirmPanel.Enter(maxSweepFloor);
                }
                else
                {
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("rush_sweep_top_floor_limit"));

                }

            }
            else
            {
                BoxManager.CreatePopupTextBox(string.Format(
                    TableTextBlo.GetText("rush_sweep_top_limit"), Dict.Blo.DictConfigBlo.GetRushSweepTopLevelLimit(),
                    Dict.Blo.DictConfigBlo.GetRushSweepTopVipLevelLimit()));
            }
        }

        public void OnRushRankButtonClick()
        {
            PageManager.Instance.OpenPage("RankPage", "type=5");
        }

        public void OnRushOneKeySweepButtonClick()
        {
            if (StaticData.playerData.level >= Dict.Blo.DictConfigBlo.GetRushSweepStarLevelLimit() ||
                StaticData.playerData.vip >= Dict.Blo.DictConfigBlo.GetRushSweepStarVipLevelLimit())
            {
                PageManager.Instance.OpenPage("RushSweepPage", "type=0");
            }
            else
            {
                BoxManager.CreatePopupTextBox(
                    string.Format(
                    TableTextBlo.GetText("rush_sweep_star_limit"),
                    Dict.Blo.DictConfigBlo.GetRushSweepStarLevelLimit(),
                    Dict.Blo.DictConfigBlo.GetRushSweepStarVipLevelLimit())
                );
                
            }

        }

        public void OnEliteBossButtonClick()
        {
            PageManager.Instance.OpenPage("RushEliteBossPage", "");
        }

        public void OnRushShopButtonClick()
        {
            PageManager.Instance.OpenPage("ShopPage", "type=" + ((int)ShopType.PointShop).ToString());
        }

        public void OnMazeTraderBtnClick()
        {
            if (isResetState)
            {
                PageManager.Instance.OpenPage("RushMazeTraderPage", "");
            }
            else
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("rush_maze_trader_limit_tip"));
            }
        }

        public void HideBtns() {
            topLeft.SetActive(false);
            topRight.SetActive(false);
            bottomRight.SetActive(false);
            bottomCenter.SetActive(false);
            bottomLeft.SetActive(false);
        }

        public void ShowBtns() {
            topLeft.SetActive(true);
            topRight.SetActive(true);
            bottomRight.SetActive(true);
            bottomCenter.SetActive(true);
            bottomLeft.SetActive(true);
        }


        void Update()
        {
        }

        #region OnRushResetConfirm
        public delegate void OnRushResetConfirm();
        public OnRushResetConfirm onRushResetConfirm;
        #endregion
    }
}
